﻿$(init);

function init() {
    //voice animation
    var $canvas = $('#voicespeaker');
    //alert($canvas.getContext);
    if (document.getElementById('voicespeaker').getContext) {
        (function () {
            var $voice = $('#voiceword');
            var context = document.getElementById('voicespeaker').getContext('2d');
            var voices = eval($voice.data('voices'));

            var x = 5;
            var y = $canvas.height() / 2;
            var r = [1, 5, 10, 16];
            var t = [0, 34, 28, 24];
            var status = 0;
            var c = 0;
            var speakcount = 0;

            window.setInterval(function () {
                var voice = voices[speakcount];

                if (c == 0) $voice.hide('fast');
                if (status == 0) {
                    context.clearRect(0, 0, $canvas.width(), $canvas.height());
                    status = 1;
                }
                else if (status == 2) {
                    c++;
                    if (c == 18) { c = 0; status = 0; }
                }
                else {
                    context.strokeStyle = '#1f70bd';
                    context.lineWidth = 2;
                    context.beginPath();

                    if (c <= 2)
                        context.arc(x, y, 1, 0, 2 * Math.PI, true);
                    else {
                        for (var i = 0; i <= c; i++) {
                            context.arc(x, y, r[i], t[i] * Math.PI / 180, i == 0 ? (2 * Math.PI) : (0 - t[i] * Math.PI / 180), true);
                            if (r[i + 1]) context.moveTo(x + r[i + 1] * Math.cos(t[i + 1] * Math.PI / 180), y + r[i + 1] * Math.sin(t[i + 1] * Math.PI / 180));
                        }
                        $voice.text(voice.author + ':' + voice.content).show();
                    }

                    status = 0;
                    context.stroke();
                    c++;
                    if (c == 4) { status = 2; }

                }

                if (speakcount >= voices.length - 1) speakcount = 0;
                else speakcount++;
            }, 250);
        })();
    }
    else {
        $('#voicespeaker').remove();
        $('#voiceword').before('<img src="/Content/images/speaker.gif" id="voicespeaker" />');
        var i = 0,speakcount = 0;
        var voices = eval($('#voiceword').data('voices'));

        setInterval(function () {
            var voice = voices[speakcount];
            if (i % 5 == 0) {
                $('#voiceword').hide('fast');
            }
            else if (i % 5 == 1) {
                $('#voiceword').text((voice.author == null ? voice.author : '') + ':' + voice.content ? voice.content : '').show();
            }
            else {
            }

            speakcount++;
            if (speakcount >= voices.length - 1) speakcount = 0;

            i++;
            if (i == 1000) i = 0;
        }, 1000);
    }
}